package com.jsfcompref.solutech.backing;

import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputSecret;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import com.eCommerce.corecontract.dto.ClientDTO;
import com.eCommerce.corecontract.dto.UserDTO;
import com.eCommerce.server.corebackend.util.BackEndConstants;
import com.eCommerce.server.corebackend.util.UtilDate;
import com.eCommercecorecontract.svco.IUserSVCO;
import com.eCommercecorecontract.svco.SVCOFinder;
import com.ecommerceWeb.common.ServiceFinder;

/**
 * Backing bean for register.jsp
 */

public class CreerClient {

	private HtmlInputText firstName;
	private HtmlInputText lastName;
	private HtmlInputText email;
	private HtmlInputText dob;
	private HtmlInputText address;
	private HtmlInputText phoneNum;
	private HtmlInputText userid;
	private HtmlInputSecret password;
	private HtmlInputSecret passwordCheck;

	public CreerClient() {
	}

	public void validateEmail(FacesContext context, UIComponent toValidate,
			Object value) throws ValidatorException {
		String eMail = (String) value;
		if (eMail.indexOf("@") < 0) {
			FacesMessage message = new FacesMessage(
					"Email invalide, saisir a nouveau!");
			throw new ValidatorException(message);
		}
	}

	public void validatePassword(FacesContext context, UIComponent toValidate,
			Object value) throws ValidatorException {

		String password1 = (String) this.getPassword().getValue();
		String password2 = (String) value;

		if (!password1.equals(password2)) {
			FacesMessage message = new FacesMessage("Verifier mot de passe!");
			throw new ValidatorException(message);
		}
	}

	public String creerClient() {

		try {
			UserDTO clientDTO = new ClientDTO();
			clientDTO.setPrenom(firstName.getValue().toString());
			clientDTO.setNom(lastName.getValue().toString());
			clientDTO.setDateNaissance(UtilDate.ConvertDateToLong((Date) dob
					.getValue()));
			clientDTO.setEmail(email.getValue().toString());
			clientDTO.setAdresse(address.getValue().toString());
			clientDTO.setTelephone(phoneNum.getValue().toString());
			clientDTO.setLogin(userid.getValue().toString());
			clientDTO.setPassword(password.getValue().toString());
			clientDTO.setPassword(passwordCheck.getValue().toString());
			SVCOFinder svcoFinder = (SVCOFinder) ServiceFinder
					.findBean("SVCOFinder");
			IUserSVCO clientSVCO = (IUserSVCO) svcoFinder
					.get(BackEndConstants.SVCO_USER);
			Long clientCreated = clientSVCO.creerUser(clientDTO);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ("confirm");

	}

	public HtmlInputText getDob() {
		return dob;
	}

	public void setDob(HtmlInputText dob) {
		this.dob = dob;
	}

	public HtmlInputText getAddress() {
		return address;
	}

	public void setAddress(HtmlInputText address) {
		this.address = address;
	}

	public HtmlInputText getPhoneNum() {
		return phoneNum;
	}

	public void setPhoneNum(HtmlInputText phoneNum) {
		this.phoneNum = phoneNum;
	}

	public void setFirstName(HtmlInputText firstName) {
		this.firstName = firstName;
	}

	public HtmlInputText getFirstName() {
		return firstName;
	}

	public void setLastName(HtmlInputText lastName) {
		this.lastName = lastName;
	}

	public HtmlInputText getLastName() {
		return lastName;
	}

	public void setEmail(HtmlInputText email) {
		this.email = email;
	}

	public HtmlInputText getEmail() {
		return email;
	}

	public void setUserid(HtmlInputText userid) {
		this.userid = userid;
	}

	public HtmlInputText getUserid() {
		return userid;
	}

	public void setPassword(HtmlInputSecret password) {
		this.password = password;
	}

	public HtmlInputSecret getPassword() {
		return password;
	}

	public void setPasswordCheck(HtmlInputSecret passwordChk) {
		this.passwordCheck = passwordChk;
	}

	public HtmlInputSecret getPasswordCheck() {
		return passwordCheck;
	}

}
